package com.wuyou.simple;

import jdk.nashorn.internal.ir.CallNode;

/**
 * 1694. 重新格式化电话号码
 */
public class ReformatNumber {


    public static void main(String[] args) {
        System.out.println("a b c".trim());
        String number = "--17-5 229 35-39475 ";
//        System.out.println(number.replace(" ", ""));
        System.out.println(solution_3(number));
    }


    public static String solution_4(String number) {
        StringBuilder res = new StringBuilder();
        // replace 2ms substring 1ms
        while (number.indexOf(" ") >= 0 || number.indexOf("-") >= 0) {
            int i1 = number.indexOf(" ");
            if (i1 >= 0) number = number.substring(0, i1) + number.substring(i1 + 1, number.length());
            int i2 = number.indexOf("-");
            if (i2 >= 0) number = number.substring(0, i2) + number.substring(i2 + 1, number.length());
        }
        int count = 0;
        while (true) {
            res.append(number.substring(res.length() - count, 3));
            count++;
            if (number.length() - res.length() - count <= 4) {
                if(number.length() - res.length() - count == 4) res.append(number.substring(res.length() - count, 2)).append(number.substring(res.length() - count));
                res.append(number.substring(res.length() - count));
                break;
            }
        }
        return res.toString();
    }

    // 1ms
    public static String solution_3(String number) {
        StringBuilder res = new StringBuilder();
        // replace 2ms substring 1ms
        while (number.indexOf(" ") >= 0 || number.indexOf("-") >= 0) {
            int i1 = number.indexOf(" ");
            if (i1 >= 0) number = number.substring(0, i1) + number.substring(i1 + 1, number.length());
            int i2 = number.indexOf("-");
            if (i2 >= 0) number = number.substring(0, i2) + number.substring(i2 + 1, number.length());
        }
        String s = number;
        while (s.length() > 4) {
            String temp = s.substring(0, 3);
            res.append(temp).append("-");
            s = s.substring(temp.length(), s.length());
        }
        if (s.length() == 4)
            return res.append(s.substring(0, 2)).append("-").append(s.substring(2, s.length())).toString();
        return res.append(s).toString();
    }

    // 2ms
    public static String solution_2(String number) {
        StringBuilder res = new StringBuilder();
        // 4ms
//        String s = number.replaceAll(" ", "").replaceAll("-", "");
        // 2ms
        String s = number.replace(" ", "").replace("-", "");
        while (s.length() > 4) {
            String temp = s.substring(0, 3);
            res.append(temp).append("-");
            s = s.substring(temp.length(), s.length());
        }
        if (s.length() == 4)
            return res.append(s.substring(0, 2)).append("-").append(s.substring(2, s.length())).toString();
        return res.append(s).toString();
    }


    // 12ms
    public static String solution_1(String number) {
        String res = "";
        String s = number.replace(" ", "").replace("-", "");
        while (s.length() > 4) {
            String temp = s.substring(0, 3);
            res += temp + "-";
            s = s.substring(temp.length(), s.length());
        }
        if (s.length() == 4) return res += s.substring(0, 2) + "-" + s.substring(2, s.length());
        return res + s;
    }
}
